// pages/cart/index.js
import {
  createStoreBindings
} from 'mobx-miniprogram-bindings'
import {
  store
} from '../../store/index.js'
import {
  getCart
} from '../../api/path/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: [

    ],
    checkedGoods: {
      price: 0,
      num: 0,
    }
  },

  onChangeGoodsAll: function (event) {
    var goodsIndex = event.currentTarget.dataset.itemIndex;
    var checked = event.detail;
    // 更新所有商品的选中状态
    this.data.goods[goodsIndex].sts = checked;
    this.data.goods[goodsIndex].list.forEach(item => {
      item.sts = checked;
    })
    // 更新页面数据
    this.setData({
      goods: this.data.goods
    });
    this.updateGoods()
  },
  onChangeGoods: function (event) {
    var goodsIndex = event.currentTarget.dataset.goodsIndex;
    var listIndex = event.currentTarget.dataset.listIndex;
    var checked = event.detail;
    // 更新单个商品的选中状态
    this.data.goods[goodsIndex].list[listIndex].sts = checked;
    // 更新页面数据
    this.setData({
      goods: this.data.goods
    });
    this.updateGoods()
  },
  updateGoods: function (event) {
    // 当 所有的 sts 都为 true 的时候，checkedAll 才为 true
    var checkedAll = this.data.goods.every(item => {
      return item.sts;
    })
    this.onChange({
      detail: checkedAll
    })

    // 计算 每一个 商品的 价格 和 数量
    var checkedGoods = {
      num: 0,
      price: 0,
      list: []
    }
    for (var i = 0; i < this.data.goods.length; i++) {
      for (var j = 0; j < this.data.goods[i].list.length; j++) {
        if (this.data.goods[i].list[j].sts) {
          checkedGoods.num += this.data.goods[i].list[j].num;
          checkedGoods.price += this.data.goods[i].list[j].num * this.data.goods[i].list[j].price;
          checkedGoods.list.push(this.data.goods[i].list[j]);
        }
      }
    }
    this.setData({
      checkedGoods: checkedGoods
    })
  },


  getCartList: function (e) {
    let userID = JSON.parse(wx.getStorageSync('userinfo')).userID
    getCart({
      userID,
    }).then(res => {
      console.log(res);
      this.setData({
        goods: res.data
      })
      var checkedGoods = {
        price: 0,
        num: 0,
      }
      res.data.forEach(item => {
        checkedGoods.price += item.priceOriginal
        checkedGoods.num += 1;
      })
      this.setData({
        checkedGoods: checkedGoods
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.storeBindings = createStoreBindings(this, {
      // 属性
      store,
      fields: [
        "checkedAll",
        "checkedGoods",
      ],
      actions: [
        "onChange",
      ]
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getCartList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    // 卸载mobx
    this.storeBindings.destroyStoreBindings()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})